import axios from 'axios';
import {useUserStore} from '@/store/user'
//1. 创建axios对象
const service = axios.create();

//2. 请求拦截器
service.interceptors.request.use(config => {
  console.log('请求拦截加请求头');
  if (useUserStore().token){
    config.headers['Authorization'] = useUserStore().token;
  }
  return config;
}, error => {
  console.log('请求拦截',error);
  
  Promise.reject(error);
});

//3. 响应拦截器
service.interceptors.response.use(response => {
  // 判断code码
  console.log('响应拦截器判断code', response.data);
  if (response.data.status === 401){
    alert('请先登录');
  }else{
    alert('正常的')
  }
  
  return response.data;
},error => {
  console.log('响应拦截器判断error', response.data);

  if (error.data.status === 401) {
    alert('请先登录');
  } else {
    alert('正常的')
  }
  return Promise.reject(error);
});

export default service;